package com.zm.zmgz.service;

import com.zm.zmgz.dao.LogBeanMapper;
import com.zm.zmgz.pojo.LogBean;
import com.zm.zmgz.pojo.LogBeanExample;
import com.zm.zmgz.pojo.LogSearchCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/04/08/0:37
 * @Description:日志查看模块业务层
 */

@Service
public class LogViewService {

    @Autowired
    LogBeanMapper logBeanMapper;

    /***
    * @Description: 获取日志对象集合
     * 无时间范围查询所有记录
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-08 02:55:10
    */
    public List<LogBean> getLogBeans(LogSearchCriteria logSearchCriteria) {
        LogBeanExample logBeanExample = new LogBeanExample();
        LogBeanExample.Criteria criteria = logBeanExample.createCriteria();
        if(!Objects.isNull(logSearchCriteria.getUserName()) && !logSearchCriteria.getUserName().equals("")) {
            criteria.andUsernameEqualTo(logSearchCriteria.getUserName());
        }
        if(!Objects.isNull(logSearchCriteria.getTableName()) && !logSearchCriteria.getTableName().equals("")) {
            criteria.andTablenameEqualTo(logSearchCriteria.getTableName());
        }
        if(!Objects.isNull(logSearchCriteria.getType()) && !logSearchCriteria.getType().equals("")) {
            criteria.andOperationtypeEqualTo(logSearchCriteria.getType());
        }
        if(!Objects.isNull(logSearchCriteria.getTimeLimitDate()) && !Objects.isNull(logSearchCriteria.getTimeLimitDate2())) {
            criteria.andOperationtimeBetween(logSearchCriteria.getTimeLimitDate(),logSearchCriteria.getTimeLimitDate2());
        }
        if(!Objects.isNull(logSearchCriteria.getPage()) && !Objects.isNull(logSearchCriteria.getLimit())) {
            logBeanExample.setOrderByClause("operationTime desc limit " + (logSearchCriteria.getPage() - 1) * logSearchCriteria.getLimit() + "," + logSearchCriteria.getLimit());
        }
        List<LogBean> logBeanList = logBeanMapper.selectByExample(logBeanExample);
        if(!Objects.isNull(logBeanList) && logBeanList.size() > 0) {
            for (LogBean logBean : logBeanList) {

                if(logBean.getOperationtype().equals("delete")){
                    logBean.setOperationtype("删除");
                } else if(logBean.getOperationtype().equals("insert")){
                    logBean.setOperationtype("新增");
                }else if(logBean.getOperationtype().equals("update")){
                    logBean.setOperationtype("更新");
                }

                if(logBean.getTablename().equals("authorityzm")){
                    logBean.setTablename("权限表");
                }else if (logBean.getTablename().equals("employeezm")){
                    logBean.setTablename("员工表");
                }else if (logBean.getTablename().equals("salarysettlementdetailszm")){
                    logBean.setTablename("结算批次明细表");
                }else if (logBean.getTablename().equals("salarysettlementzm")){
                    logBean.setTablename("结算批次表");
                }else if (logBean.getTablename().equals("userzm")){
                    logBean.setTablename("用户表");
                }

            }
            System.out.println("日志对象集合:"+logBeanList);
            return logBeanList;
        }else {
            System.err.println("获取日志对象集合失败!");
            return null;
        }
    }


    /***
     * @Description: 获取总记录数
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-08 03:33:12
     */
    public List<LogBean> getNumbers(LogSearchCriteria logSearchCriteria) {
        LogBeanExample logBeanExample = new LogBeanExample();
        LogBeanExample.Criteria criteria = logBeanExample.createCriteria();
        if(!Objects.isNull(logSearchCriteria.getUserName()) && !logSearchCriteria.getUserName().equals("")) {
            criteria.andUsernameEqualTo(logSearchCriteria.getUserName());
        }
        if(!Objects.isNull(logSearchCriteria.getTableName()) && !logSearchCriteria.getTableName().equals("")) {
            criteria.andTablenameEqualTo(logSearchCriteria.getTableName());
        }
        if(!Objects.isNull(logSearchCriteria.getType()) && !logSearchCriteria.getType().equals("")) {
            criteria.andOperationtypeEqualTo(logSearchCriteria.getType());
        }
        if(!Objects.isNull(logSearchCriteria.getTimeLimitDate()) && !Objects.isNull(logSearchCriteria.getTimeLimitDate2())) {
            criteria.andOperationtimeBetween(logSearchCriteria.getTimeLimitDate(),logSearchCriteria.getTimeLimitDate2());
        }

        List<LogBean> logBeanList = logBeanMapper.selectByExample(logBeanExample);
        if(!Objects.isNull(logBeanList) && logBeanList.size() > 0) {
            for (LogBean logBean : logBeanList) {

                if(logBean.getOperationtype().equals("delete")){
                    logBean.setOperationtype("删除");
                } else if(logBean.getOperationtype().equals("insert")){
                    logBean.setOperationtype("新增");
                }else if(logBean.getOperationtype().equals("update")){
                    logBean.setOperationtype("更新");
                }

                if(logBean.getTablename().equals("authorityzm")){
                    logBean.setTablename("权限表");
                }else if (logBean.getTablename().equals("employeezm")){
                    logBean.setTablename("员工表");
                }else if (logBean.getTablename().equals("salarysettlementdetailszm")){
                    logBean.setTablename("结算批次明细表");
                }else if (logBean.getTablename().equals("salarysettlementzm")){
                    logBean.setTablename("结算批次表");
                }else if (logBean.getTablename().equals("userzm")){
                    logBean.setTablename("用户表");
                }

            }
            System.out.println("日志对象集合:"+logBeanList);
            return logBeanList;
        }else {
            System.err.println("获取日志对象集合失败!");
            return null;
        }
    }
}
